﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.ServiceModel;
using System.ServiceModel.Security;
using AVAilableRooms.Exchange;
using System.Collections.Generic;
using System.ServiceModel.Description;

namespace AVAilableRooms.Services
{
    public class ExchangeService
    {
        public ExchangeServicePortTypeClient _ExchangeService;

        #region Properties

        /*
        public RequestServerVersion RequestServerVersionValue
        {
            get { return _ExchangeService.RequestServerVersionValue; }
            set { _ExchangeService.RequestServerVersionValue = value; }
        }
        */
        
        #endregion

        public ExchangeService(String Url, NetworkCredential Credential)
        {
            /*
            ServicePointManager.ServerCertificateValidationCallback =
            delegate(Object obj, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors)
            {
                return true;
            };
            

            BasicHttpBinding binding = new BasicHttpBinding();
            binding.Security.Mode = BasicHttpSecurityMode.Transport;
            
            EndpointAddress endpoint = new EndpointAddress(new Uri(Url));
            
            _ExchangeService = new ExchangeServicePortTypeClient(binding, endpoint);
            _ExchangeService.ClientCredentials.UserName.UserName = Credential.UserName;
            _ExchangeService.ClientCredentials.UserName.Password = Credential.Password;
             */
        }

        /*
        public void GetRoomLists()
        {
            _ExchangeService.GetRoomListsCompleted += new EventHandler<GetRoomListsCompletedEventArgs>(_GetRoomListsCompleted);
            _ExchangeService.GetRoomListsAsync(new GetRoomListsType());
        }

        private void _GetRoomListsCompleted(object sender, GetRoomListsCompletedEventArgs e)
        {
            if (e.Result.RoomLists != null)
                return e.Result.RoomLists;

            return new EmailAddressType[0];
        }
        /*
        public List<RoomType> GetRooms(EmailAddressType emailAddressType)
        {
            GetRoomsResponseMessageType response = _ExchangeService.GetRooms(new GetRoomsType() { RoomList = emailAddressType });

            if(response.Rooms != null)
                return response.Rooms.ToList<RoomType>();

            return new List<RoomType>();
        }

        public List<CalendarEvent> GetRoomAvailability(List<MailboxData> MailBoxes, SerializableTimeZone TimeZone, FreeBusyViewOptionsType fbViewOptions)
        {
            // Make the request.
            GetUserAvailabilityRequestType request = new GetUserAvailabilityRequestType();

            // Set the time zone of the request.
            request.TimeZone = TimeZone;

            // Add the mailboxes to the request.
            request.MailboxDataArray = MailBoxes.ToArray();

            // Add the free/busy view options to the request.
            request.FreeBusyViewOptions = fbViewOptions;

            GetUserAvailabilityResponseType response = _ExchangeService.GetUserAvailability(request);

            if(response.FreeBusyResponseArray.FirstOrDefault().FreeBusyView.CalendarEventArray != null)
                return response.FreeBusyResponseArray.FirstOrDefault().FreeBusyView.CalendarEventArray.ToList<CalendarEvent>();

            return new List<CalendarEvent>();
        }
        */
    }
}
